<template>
  <div class="add-or-update mod-menu">
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px" v-loading="loading">
      <el-form-item label="上级菜单:" prop="parentName">
        <el-popover
          v-model="popoverVisible"
          ref="menuListPopover"
          placement="bottom-start"
          trigger="click">
          <div class="selector">
          <el-tree
            :data="menuList"
            :props="array"
            node-key="id"
            ref="menuListTree"
            @current-change="menuListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
          </div>
        </el-popover>
        <el-input v-model="dataForm.parentName" v-popover:menuListPopover :readonly="true" placeholder="点击选择上级菜单" class="menu-list__input"></el-input>
      </el-form-item>
      <el-form-item label="名称:" prop="name">
        <el-input placeholder="请输入名称" v-model="dataForm.name">
        </el-input>
      </el-form-item>
      <el-form-item label="授权标识:" prop="permission">
        <el-input placeholder="请输入授权标识" v-model="dataForm.permission">
        </el-input>
      </el-form-item>
      <el-form-item label="访问地址:" prop="href">
        <el-input placeholder="请输入地址" v-model="dataForm.href">

        </el-input>
      </el-form-item>
      <el-form-item label="图标:" prop="icon">
        <el-row>
          <el-col :span="24">
            <el-popover
             v-model="iconVisible"
              ref="iconListPopover"
              placement="bottom"
              trigger="click"
              popper-class="mod-menu__icon-popover"
              >
              <div class="mod-menu__icon-list">
                <el-button
                  v-for="(item, index) in iconList"
                  :key="index"
                  @click="iconActiveHandle(item)"
                  :class="{ 'is-active': item === dataForm.icon }">
                  <icon-svg :name="item"></icon-svg>
                </el-button>
              </div>
            </el-popover>
            <el-input v-model="dataForm.icon" v-popover:iconListPopover :readonly="true" placeholder="菜单图标名称" class="icon-list__input" ></el-input>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="排序码:" prop="sort">
        <el-input-number v-model="dataForm.sort" controls-position="right" :min="0"></el-input-number>
      </el-form-item>
      <el-form-item>
          <div style="margin-top: 20px">
            <el-radio v-model="dataForm.isShow" label="1" border size="medium">显示</el-radio>
            <el-radio v-model="dataForm.isShow" label="0" border size="medium">不显示</el-radio>
          </div>
      </el-form-item>
      <el-form-item label="备注:" prop="remarks">
        <el-input type="textarea" placeholder="请输入备注" :rows=4  v-model="dataForm.remarks">

        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="dataFormSubmit()">提交</el-button>
        <el-button @click="resetForm('dataForm')">重置</el-button>
        <el-button @click="back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>


</template>

<script>
import Icon from '@/icons'
export default {
  data () {
    return {
      popoverVisible: false,
      iconVisible: false,
      loading: false,
      dataForm: {
        id: 0,
        parentId: 0,
        parentName: '',
        name: '',
        sort: '',
        href: '',
        target: '',
        icon: '',
        permission: '',
        remarks: '',
        children: '',
        isShow: '',
        iconList: []
      },
      dataRule: {
        parentName: [
          { required: true, message: '上级菜单不能为空', trigger: 'change' }
        ],
        name: [
          { required: true, message: '菜单名称不能为空', trigger: 'blur' }
        ],
        permission: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' }
        ],
        icon: [{ required: true, message: '图标不能为空', trigger: 'change' }],
        sort: [{ required: true, message: '排序码不能为空', trigger: 'blur' }]
      },
      menuList: [],
      counter: 0,
      array: {
        value: 'id',
        label: 'name'
      }
    }
  },
  activated () {
    let id = this.$route.params.id || 0
    console.log('id', id)
    this.dataForm.id = id
    this.init(id)
  },
  created () {
    this.iconList = Icon.getNameList()
    let id = this.$route.params.id || 0
    console.log('id', id)
    this.dataForm.id = id
    this.init(id)
  },
  methods: {
    // 根据是新增还是修改对表单中的字段进行处理
    init (id) {
      this.loading = true
      this.dataForm.id = id || 0
      console.log(this.dataForm.id)
      this.$http({
        url: this.$http.adornUrl('/sysMenu/findAllMenu'),
        method: 'get',
        params: this.$http.adornParams()
      })
        .then(({ data }) => {
          this.loading = false
          let menu = {
            id: '-1',
            name: '顶级菜单',
            children: []
          }
          console.log(data)
          menu.children = data
          console.log(menu)
          this.menuList = []
          this.menuList.push(menu)
        })
          .then(() => {
            if (this.dataForm.id) {
              this.loading = true
              // 修改
              this.$http({
                url: this.$http.adornUrl(`/sysMenu/query/detail`),
                method: 'get',
                params: this.$http.adornParams({id: id})
              }).then(({data}) => {
                this.loading = false
                this.dataForm.name = data.name
                this.dataForm.permission = data.permission
                this.dataForm.href = data.href
                this.dataForm.icon = data.icon
                this.dataForm.sort = data.sort
                this.dataForm.parentId = data.parentId
                this.dataForm.remarks = data.remarks
                this.menuListTreeSetCurrentNode()
              })
            } else {
              // 新增是清空表单
              this.resetForm('dataForm')
            }
          })
    },
      // 选中图标
    iconActiveHandle (iconName) {
      this.dataForm.icon = iconName
      this.iconVisible = false
    },
      // 表单重置
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    back () {
      this.$router.push({name: 'repaire-repaireOrder'})
    },
      // 菜单树选中
    menuListTreeCurrentChangeHandle (data, node) {
      this.dataForm.parentId = data.id
      this.dataForm.parentName = data.name
      this.popoverVisible = false
    },
      // 菜单树设置当前选中节点
    menuListTreeSetCurrentNode () {
      this.$refs.menuListTree.setCurrentKey(this.dataForm.parentId)
      this.dataForm.parentName = (this.$refs.menuListTree.getCurrentNode() || {})['name']
    },
      // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.loading = true
          this.$http({
            url: this.$http.adornUrl('/sysMenu/save'),
            method: 'post',
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
            },
            data: this.$http.adornData(
              {
                id: this.dataForm.id || undefined,
                name: this.dataForm.name,
                parentId: this.dataForm.parentId,
                sort: this.dataForm.sort,
                href: this.dataForm.href,
                icon: this.dataForm.icon,
                isShow: this.dataForm.isShow,
                permission: this.dataForm.permission
              },
                false,
                'form'
              )
          })
              .then(() => {
                this.loading = false
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500  // 持续时间
                })
                this.$router.push({name: 'sys-menu'})
              })
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
  .mod-menu{
    .menu-list__input,
    .icon-list__input {
      > .el-input__inner {
        cursor: pointer;
      }
    }
    &__icon-list {
      min-width: 370px;
      max-width: 500px;
      padding: 0;
      margin: -8px 0 0 -8px;
      > .el-button {
        padding: 8px;
        margin: 8px 0 0 8px;
        > span {
          display: inline-block;
          vertical-align: middle;
          width: 18px;
          height: 18px;
          font-size: 18px;
        }
      }
    }
  }
</style>
